<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 23.03.13
 * Time: 19:45
 * To change this template use File | Settings | File Templates.
 */
$PATH = Config::getInstance()->getBasePath();
require_once("$PATH/persistence/SearchFilmCriteria.php");
require_once("$PATH/dao/AbstractDao.php");
require_once("$PATH/controller/FilmController.php");
require_once("$PATH/dao/ActorDao.php");
require_once("$PATH/dao/CountryDao.php");
require_once("$PATH/dao/GenreDao.php");

class FilmDao extends AbstractDao
{
    function __construct()
    {
        $this->controller = new FilmController();
    }

    public function loadByCriteria(SearchFilmCriteria $criteria)
    {
        // TODO: Need realize method

        /*if (count($criteria->getActors()) == 0) {
            $actorDao = new ActorDao();
            $actors = $actorDao->load();
            $actorsArray = array();
            foreach ($actors as $item)
                array_push($actorsArray, $item->getId());
            $criteria->setActors($actorsArray);
        }
        if (count($criteria->getCountries()) == 0) {
            $countryDao = new CountryDao();
            $counties = $countryDao->load();
            $countiesArray = array();
            foreach ($counties as $item)
                array_push($countiesArray, $item->getId());
            $criteria->setCountries($countiesArray);
        }
        if (count($criteria->getGenres()) == 0) {
            $genreDao = new GenreDao();
            $genres = $genreDao->load();
            $genresArray = array();
            foreach ($genres as $item)
                array_push($genresArray, $item->getId());
            $criteria->setGenres($genresArray);
        }*/

        $queryText =
            "SELECT DISTINCT " .
                "films.id, films.name, films.descr, films.year " .
            "FROM films ";

        if (count($criteria->getActors()) > 0)
            $queryText = $queryText .
                "INNER JOIN rel_film_actor " .
                "ON rel_film_actor.film_id = films.id AND " .
                "rel_film_actor.actor_id IN (#actors#) ";
        if (count($criteria->getCountries()) > 0)
            $queryText = $queryText .
                "INNER JOIN rel_film_country " .
                "ON rel_film_country.film_id = films.id AND " .
                "rel_film_country.county_id IN (#countries#) ";
        if (count($criteria->getGenres()) > 0)
            $queryText = $queryText .
                "INNER JOIN rel_film_genre " .
                "ON rel_film_genre.film_id = films.id AND " .
                "rel_film_genre.genre_id IN (#genres#) ";


        // Build map for replacing
        $mapping = array(
            "name"          => $criteria->getName(),
            "start_date"    => $criteria->getYearFrom(),
            "end_date"      => $criteria->getYearTo(),
            "actors"        => PatternUtil::getInstance()->getValues($criteria->getActors()),
            "countries"     => PatternUtil::getInstance()->getValues($criteria->getCountries()),
            "genres"        => PatternUtil::getInstance()->getValues($criteria->getGenres())
        );

        // Create query
        /*$query = PatternUtil::getInstance()->merge(
            "SELECT DISTINCT films.id, films.name, films.descr, films.year " .
            "FROM films " .
            "INNER JOIN rel_film_actor " .
                "ON rel_film_actor.film_id = films.id AND " .
                "rel_film_actor.actor_id IN (#actors#) " .
            "INNER JOIN rel_film_country " .
                "ON rel_film_country.film_id = films.id AND " .
                "rel_film_country.county_id IN (#countries#) " .
            "INNER JOIN rel_film_genre " .
                "ON rel_film_genre.film_id = films.id AND " .
                "rel_film_genre.genre_id IN (#genres#) " .
            "WHERE " .
                "films.name LIKE '%#name#%' AND " .
                "films.year >= #end_date# AND " .
                "films.year <= #start_date#",
            $mapping);*/

        $query = PatternUtil::getInstance()->merge(
            $queryText .
            "WHERE " .
                "films.name LIKE '%#name#%' AND " .
                "films.year >= #start_date# AND " .
                "films.year <= #end_date#",
            $mapping);

        // Load data from database
        $result = mysql_query($query, Config::getInstance()->getDbConnection());

        // Build object's array
        $instances = array();
        while ($row = mysql_fetch_array($result)) {
            $instance = PatternUtil::getInstance()->getObjectFromArray(
                $this->controller, $row);
            array_push($instances, $instance);
        }
        return $instances;
    }

    public function insertAndRelative($instance, $actors, $genres, $countries)
    {
        $instId = $this->insert($instance);

        foreach ($actors as $key => $value) {
            $query = "INSERT INTO rel_film_actor (film_id, actor_id) VALUES (" . $instId . ", " . $value . ")";
            mysql_query($query, Config::getInstance()->getDbConnection());
        }

        foreach ($genres as $key => $value) {
            $query = "INSERT INTO rel_film_genre (film_id, genre_id) VALUES (" . $instId . ", " . $value . ")";
            mysql_query($query, Config::getInstance()->getDbConnection());
        }

        foreach ($countries as $key => $value) {
            $query = "INSERT INTO rel_film_country (film_id, country_id) VALUES (" . $instId . ", " . $value . ")";
            mysql_query($query, Config::getInstance()->getDbConnection());
        }
    }
}
